//
//  PCSchoolAppDelegate.m
//  PCSchool
//
//  Created by Jayanta on 07/07/13.
//  Copyright (c) 2013 Jayanta. All rights reserved.
//

#import "PCSchoolAppDelegate.h"
#import "LogInViewController.h"
#import "CustomTabBarView.h"
#import "StudentListViewController.h"
#import "SubjectViewController.h"
#import "AbsentViewController.h"
#import "CalendarViewController.h"
#import "AppraisalViewController.h"
#import "ConnectionManager.h"
#import "DataModel.h"
#import "MBProgressHUD.h"


@implementation PCSchoolAppDelegate

@synthesize tabBarController,dataModel,deviceID,userDefault;

- (void)dealloc
{
    [_window release];
    [tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.userDefault = [NSUserDefaults standardUserDefaults];

    NSUUID *UDID = [[UIDevice currentDevice] identifierForVendor];
    self.deviceID = [UDID UUIDString];
    
    self.closestStudentDetails = [NSArray array];
    self.arrayForStudentDetails = [NSArray array];
    
    DataModel* aDataModel = [[DataModel alloc] init];
	[self setDataModel:aDataModel];
	[aDataModel release];
    
    NSLog(@"[self.userDefault valueForKey:LOGIN_SUCCESS]  %d",[self.userDefault boolForKey:LOGIN_SUCCESS]);
    
//    if ([self.userDefault boolForKey:LOGIN_SUCCESS])
//    {
//        
//        self.arrayForStudentDetails = [self.userDefault objectForKey:LOGIN_USER_DETAILS];
//        
//        StudentListViewController *studentViewController=[[StudentListViewController alloc] initWithNibName:@"StudentListViewController" bundle:Nil];
//        UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:studentViewController];
//        self.window.rootViewController=navigationController;
//        [studentViewController release];
//    }
//    else
    {
        LogInViewController *loginViewController=[[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:Nil];
        UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:loginViewController];
        self.window.rootViewController=navigationController;
        [loginViewController release];
    }
    
    
    
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


-(void) addTabBarController
{
    self.window.rootViewController = nil;
    
    
    StudentListViewController *viewController1 = [[StudentListViewController alloc] init];
    viewController1.title = @"Student";
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    [viewController1 release];
    
    self.window.rootViewController = navigationController1;
    
    [self.window makeKeyAndVisible];

    /*self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [self initializeTabBarItems];
    
    self.window.rootViewController = self.tabBarController;
    [CustomTabBarView setupTabBarViewOnTabBarController:self.tabBarController];*/

}

- (NSArray *)initializeTabBarItems
{
    NSArray * retval;
    
    
    /* Initialize view controllers */
    
    StudentListViewController *viewController1 = [[StudentListViewController alloc] init];
    viewController1.title = @"Student";
    
    SubjectViewController *viewController2 = [[SubjectViewController alloc] init];
    viewController2.title = @"Subject";
    
    AbsentViewController *viewController3 = [[AbsentViewController alloc] init];
    viewController3.title = @"Absent";
    
    CalendarViewController *viewController4 = [[CalendarViewController alloc] init];
    viewController4.title = @"Calendar";
    
    AppraisalViewController *viewController5 = [[AppraisalViewController alloc] init];
    viewController5.title = @"Appraisal";
    
    /* Initialize navigation controllers */
    
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    
    UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
    
    UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
    
    UINavigationController *navigationController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
    
    
    /*  Release View Controllers */
    
    [viewController1 release];
    [viewController2 release];
    [viewController3 release];
    [viewController4 release];
    [viewController5 release];

    
    /* Stuff Navigation Controllers into return value */
    
    retval = [NSArray arrayWithObjects:navigationController1,navigationController2,viewController3,viewController4,viewController5,nil];
    
    
    /* Release Navigation Controllers */
    
    [navigationController1 release];
    [navigationController2 release];
    [navigationController3 release];
    [navigationController4 release];
    [navigationController5 release];

    
    return (retval);
    
}


@end
